///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////

package controller {

    import model.CurrencyRatesProxy;
    import model.ElfinProxy;
    import model.UserProxy;
    import model.vo.UserVO;

    import org.kolonitsky.alexey.debug.DebugWrapper;

    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.command.SimpleCommand;

    /**
     * @author: akolonitsky
     * @date: 12/7/11 3:05 PM
     */
    public class CurrencyRatesGetCommand extends SimpleCommand
    {
        override public function execute(notification:INotification):void
        {
            super.execute(notification);

            var userProxy:UserProxy = facade.retrieveProxy(UserProxy.NAME) as UserProxy;
            var user:UserVO = userProxy.user;

            var currencies:Array = [];
            for (var p:String in user.usedCurrencies)
                currencies.push(p);

            if (currencies.length < 2)
            {
                DebugWrapper.trace("INFO: CMD > CurrencyRatesGetCommand. Don't crate proxy. Used one currency");
                return;
            }

            DebugWrapper.trace("INFO: CMD > CurrencyRatesGetCommand. for " + currencies);

            // register proxy or retrieve it
            var ratesProxy:CurrencyRatesProxy = null;
            if (facade.hasProxy(CurrencyRatesProxy.NAME))
            {
                ratesProxy = facade.retrieveProxy(CurrencyRatesProxy.NAME) as CurrencyRatesProxy;
            }
            else
            {
                var elfinProxy:ElfinProxy = facade.retrieveProxy(ElfinProxy.NAME) as ElfinProxy;
                ratesProxy = new CurrencyRatesProxy(elfinProxy.config)
                facade.registerProxy(ratesProxy);
            }

            // get rates
            ratesProxy.getRates(currencies, user.createAccountDate);
        }
    }
}
